package com.unpay.other;

import com.umpay.bean.Const;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

public class LoginService extends Service {
    
    // 登录
    public static final String TYPE_LOGIN = "type_login";
    // 注销
    public static final String TYPE_LOGOUT = "type_logout";
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("kk", "other onStart:" + intent.toUri(0));
        if (intent != null) {
            String type = intent.getStringExtra("type");
            if (TextUtils.equals(type, TYPE_LOGIN)) {
                // todo 登录
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        Log.e("kk", "打开登录activity:" + System.currentTimeMillis());
                        String action = Const.ACTION_LOGIN;
                        startActivity(action);
                    }
                }, 5000);


                // Intent in = new Intent(LoginService.this, MainActivity.class);
                // in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // in.putExtra("type", TYPE_LOGIN);
                // startActivity(in);
            } else if (TextUtils.equals(type, TYPE_LOGOUT)) {
                // todo 注销成功后 通知Launcher
                Intent in = new Intent();
                in.setAction(Const.ACTION_LAUNCHER);
                in.setPackage(Const.PACKAGE_LAUNCHER);
                in.putExtra("type", Const.TYPE_LOGOUT);
                in.putExtra("status", 0);
                in.putExtra("des", "退出成功");
                in.putExtra("data", "");
                startService(in);
                LoginService.this.startService(in);
            }
        }
        return START_REDELIVER_INTENT;
    }
    
    /**
     * 打开activity
     *
     * @param action 跳转的activity的清单文件的 action
     */
    public void startActivity(String action) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
}
